import { Component, OnInit, Input, Injector } from '@angular/core';
import { HttpClient } from "@angular/common/http";
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { Observable } from 'rxjs/Observable';
import { debounceTime } from 'rxjs/operators/debounceTime';
import { map } from 'rxjs/operators/map';
import { switchMap } from 'rxjs/operators/switchMap';
import { ActivatedRoute } from '@angular/router';
import { NzMessageService } from 'ng-zorro-antd';
import { importExpr } from '@angular/compiler/src/output/output_ast';
import { environment } from 'environments/environment';
import {
  PagedResultDto, ClassInfoOutput, PagerSearchInput, HttpService, BaseComponentConfig,
  CustomNavigation, KeyValueOutput, SearchValue
} from '../../..';
import { NgZorroPageBaseComponent } from '../../../core/ng-zorro-page-base-component/ng-zorro-page-base-component.component';
import { StudentJoinCourseOutput } from '../../../core/model/StudentJoinCourseDto';
import { isNumber } from 'util';

@Component({
  selector: 'app-student-join-course',
  templateUrl: './student-join-course.component.html',
  styleUrls: ['../../../core/ng-zorro-page-base-component/ng-zorro-page-base-component.component.scss']
})
export class StudentJoinCourseComponent extends NgZorroPageBaseComponent implements OnInit {
  listUrl = 'ClassInfo/GetClassByName';
  searchChange$ = new BehaviorSubject(new SearchValue('', ''));
  optionList = [];
  selectedUser;
  /**
   * 是否从远程获取数据
   */
  isLoading = false;

  onSearch(value: string, url: string): void {
    this.isLoading = true;
    this.searchChange$.next(new SearchValue(value, this.listUrl));
  }

  constructor(
    injecter: Injector,
    public http: HttpClient) {
    super(injecter,
      new BaseComponentConfig(
        "Course/GetCourseByStudent",
        "",
        "",
        "",
        "",
        "id",
        "",
        "我的课程",
        [],
        "courseInfoId",
        { Edit: false, Dell: false },
      ));
  }
  ngOnInit() {
    this.IniObserver.subscribe(sb => {
      this.getEntityList();
    });
  }
  getNewEntity(): StudentJoinCourseOutput {
    return new StudentJoinCourseOutput();
  }

  async joinCourse(value: string | number) {
    if (isNumber(value)) {
      this.isLoading = true;
      try {
        await this.httpService.post('Course/StudentJoinCourse', {
          "id": 0,
          "ids": [value]
        });
        await this.getEntityList();
        this.refreshStatus();
      } catch (error) {
      }
      this.isLoading = false;
    } else {
      const thisInstance = this;
      const itemKey = [];
      this.listEntities.entities.filter(e => e.IsChecked).forEach(e => {
        itemKey.push(e[this.componentConfig.KeyFieldName]);
      });
      const body = {
        "id": 0,
        "ids": itemKey
      };
      this.nzModalService.confirm({
        nzTitle: '您是否要加入选中的课程',
        nzContent: `已经选中的：${itemKey.length}项内容？`,
        nzZIndex: 1010,
        nzOnOk() {
          return new Promise(async (resolve, reject) => {
            try {
              await thisInstance.httpService.post('Course/StudentJoinCourse', body);
              await thisInstance.getEntityList();
              thisInstance.refreshStatus();
              resolve();
            } catch (error) {
              reject();
            }
          }).catch(e => {
          });
        }
      });
    }
  }
}
